﻿    using System;
using System.Collections.Generic;

namespace ErgoBreak.Core.Resources
{
    public class ResourceCollection : IDisposable
    {
        private IDictionary<string, object> resources;

        public ResourceCollection(IDictionary<string, object> collection)
        {
            this.resources = collection;
        }

        public object GetResource(string resourceName)
        {
            try
            {
                return resources[resourceName];
            }
            catch (KeyNotFoundException kEx)
            {
                throw new ResourceNotFoundException(
                    string.Format("Resource '{0}' could not be found in the resources.", resourceName));
            }
        }

        public void Dispose()
        {
            IEnumerator<KeyValuePair<string, object>> itr = this.resources.GetEnumerator();
            IDisposable trash = null;

            // clean up 
            while (itr.MoveNext())
            {
                trash = itr.Current.Value as IDisposable;
                if (trash != null)
                    trash.Dispose();
            }
        }
    }
}
